﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Comm;
using VehicleMaintenance.Models;

namespace VehicleMaintenance.BaseInfo
{
	public partial class CarInfoForm : BaseForm
	{

		#region 私有变量及属性

		/// <summary>
		/// 当前窗口状态
		/// </summary>
		public State state = State.Add;

		private CarInfo car;

		/// <summary>
		/// 当前添加车辆，只在根据车牌号创建车辆档案状态下有效
		/// </summary>
		public CarInfo Car
		{
			get { return car; }
			set { car = value; }
		}

		/// <summary>
		/// 记录当前车辆ID
		/// </summary>
		private int carID = 0;

		/// <summary>
		/// 记录当前添加的车牌号
		/// </summary>
		private string strNO = "";

		#endregion 私有变量及属性

		public CarInfoForm()
		{
			InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.CarInfo));

			InitializeSepcial();
		}

		/// <summary>
		/// 传递车牌号来新建车辆档案
		/// </summary>
		/// <param name="no">车牌号</param>
		public CarInfoForm(string no)
		{
			InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.CarInfo));
			InitializeSepcial();

			state = State.Add;
			strNO = no;
		}

		public CarInfoForm(int id)
		{
			InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.CarInfo));

			InitializeSepcial();

			carID = id;
			state = State.Edit;
		}

		private void CarInfoForm_Load(object sender, EventArgs e)
		{
			try
			{
				if (this.state == State.Edit)
				{
					CarDBContext db = new CarDBContext();

					CarInfo carInfo = db.CarInfo.Include("Customer").Include("CarType").First(p => p.CarID == this.carID);
					ucFormInput1.SetValues(carInfo);

					ucFormInput1.GetControlByName("CustomerID").Text = carInfo.Customer.Name;
					ucFormInput1.GetControlByName("CarTypeID").Text = carInfo.CarType.Name;
				}
				else
				{
					ucFormInput1.ClearData();
					if (string.IsNullOrEmpty(strNO))
					{
						InitializeDefault();
					}
					else
					{
						ucFormInput1.GetControlByName("NO").Text = strNO;
						ucFormInput1.GetControlByName("NO").Focus();
					}
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				CarDBContext db = new CarDBContext();

				if (!ucFormInput1.VerifyInput())
				{
					return;
				}

				if (this.state == State.Edit)
				{
					CarInfo carInfo = db.CarInfo.First(p => p.CarID == carID);
					ucFormInput1.GetValues(carInfo);

					db.SaveChanges();

					this.ShowInfo("保存成功！");
				}
				else
				{
					//查询车牌号是否重复
					string strCode = ucFormInput1.GetControlByName("NO").Text;
					if (db.CarInfo.FirstOrDefault(p => p.NO == strCode) != null)
					{
						this.ShowInfo("车牌号已经存在");
						//tabPanel.Controls["Code"]();
						ucFormInput1.GetControlByName("NO").Focus();
						return;
					}
					var carInfo = db.CarInfo.Create();

					ucFormInput1.GetValues(carInfo);

					db.CarInfo.Add(carInfo);
					db.SaveChanges();

					car = carInfo;

					if (string.IsNullOrEmpty(strNO))
					{
						if (MessageBox.Show(this, "保存成功，是否继续添加？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
						{
							ucFormInput1.ClearData();
							InitializeDefault();

						}
						else
						{
							this.DialogResult = System.Windows.Forms.DialogResult.OK;
						}
					}
					else
					{
						this.DialogResult = System.Windows.Forms.DialogResult.OK;
					}
				}

			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			try
			{
				this.Close();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void InitializeSepcial()
		{
			try
			{
				ucFormInput1.GetControlByName("CustomerID").Click += new EventHandler(customer_Click);
				ucFormInput1.GetControlByName("CarTypeID").Click += new EventHandler(carType_Click);
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}


		void customer_Click(object sender, EventArgs e)
		{
			try
			{


				TextBox txt = (sender as TextBox);

				CustomerListForm form = new CustomerListForm();
				if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
				{
					txt.Text = form.Customer.Name;
					txt.Tag = form.Customer.CustomerID;
				}


			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		void carType_Click(object sender, EventArgs e)
		{
			try
			{
				TextBox txt = (sender as TextBox);

				CarTypeForm form = new CarTypeForm();
				if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
				{
					txt.Text = form.CarType.Name;
					txt.Tag = form.CarType.CarTypeID;

					ucFormInput1.GetControlByName("CarName").Text = ucFormInput1.GetControlByName("CustomerID").Text + "-" + form.CarType.Name;
				}

			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}
		void InitializeDefault()
		{
			ucFormInput1.GetControlByName("NO").Text = "冀A";
			ucFormInput1.GetControlByName("NO").Focus();
		}
	}
}
